package com.fz.song.feibao.web.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.song.feibao.framework.common.RedisKey;
import com.fz.song.feibao.framework.common.enums.CodeEnum;
import com.fz.song.feibao.framework.common.enums.DelEnum;
import com.fz.song.feibao.framework.common.enums.MemberStateEnum;
import com.fz.song.feibao.framework.exceptions.ServerException;
import com.fz.song.feibao.web.entity.Member;
import com.fz.song.feibao.web.entity.vo.register.req.MemberRegisterReqVO;
import com.fz.song.feibao.web.mapper.MemberMapper;
import com.fz.song.feibao.web.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2021-11-01
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {


    @Value("${feibao.member.user-key}")
    private String userKey;


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 检查会员是否存在
     *
     * @param userName
     * @return
     */
    @Override
    public Boolean userNameIsExist(String userName) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getUsername, userName);
        wrapper.eq(Member::getDel, DelEnum.NOT_DEL.getVal());
        return getOne(wrapper) != null;
    }

    /**
     * 校验手机号是否存在
     *
     * @param mobile
     * @return
     */
    @Override
    public Boolean mobileIsExist(String mobile) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getMobile, mobile);
        wrapper.eq(Member::getDel, DelEnum.NOT_DEL.getVal());
        return getOne(wrapper) != null;
    }

    /**
     * 校验昵称是否存在
     *
     * @param nickname
     * @return
     */
    @Override
    public Boolean nicknameIsExist(String nickname) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getNickname, nickname);
        wrapper.eq(Member::getDel, DelEnum.NOT_DEL.getVal());
        return getOne(wrapper) != null;
    }

    /**
     * 会员注册
     *
     * @param req 注册参数
     * @return
     */
    @Override
    public Boolean register(MemberRegisterReqVO req) {
        String username = req.getUsername();
        String password = req.getPassword();
        String mobile = req.getMobile();
        String nickname = req.getNickname();

        String key = RedisKey.VERIFY_CODE + mobile;
        String verifyCode = req.getVerifyCode();

        String val = redisTemplate.opsForValue().get(key);
        if (StrUtil.isBlank(val) || !val.equals(verifyCode)) {
            throw new ServerException(CodeEnum.VERIFY_ERROR);
        }

        if (userNameIsExist(username) || mobileIsExist(mobile)) {
            throw new ServerException(CodeEnum.USER_IS_EXIST);
        }
        if (mobileIsExist(mobile)) {
            throw new ServerException(CodeEnum.MOBILE_IS_EXIST);
        }
        if (nicknameIsExist(nickname)) {
            throw new ServerException(CodeEnum.NICKNAME_IS_EXIST);
        }
        Member member = new Member();
        member.setNickname(nickname);
        member.setUsername(username);
        member.setPassword(SaSecureUtil.md5BySalt(password, userKey));
        member.setMobile(mobile);
        member.setGender(0);
        member.setRegisterTime(LocalDateTime.now());
        member.setCreateTime(LocalDateTime.now());
        member.setUpdateTime(LocalDateTime.now());
        member.setDel(DelEnum.NOT_DEL.getVal());
        member.setState(MemberStateEnum.NORMAL.getVal());
        boolean save = save(member);
        if (save) {
            redisTemplate.delete(key);
        }
        return save;
    }

    /**
     * 根据用户名查询会员
     *
     * @param username
     * @return
     */
    @Override
    public Member queryMemberUserName(String username) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getUsername, username);
        wrapper.eq(Member::getDel, DelEnum.NOT_DEL.getVal());
        return getOne(wrapper);
    }
}
